﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmDieuChuyenKho : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private bool allowCloseForm = true;
        public static int daluu = 0;
        public static MaVachBO MaVachBO = MaVachBO.NewRootMaVachBO();
        private TonKhoBO tonKhoBON = TonKhoBO.NewRootTonKhoBO();
        private TonKhoBO tonKhoBOD = TonKhoBO.NewRootTonKhoBO();
        private MaVachBO MaVachBOD = MaVachBO.NewRootMaVachBO();

        private double soluongChuyen = 0;
        private double soluongNguon = 0;
        private double soluongDich = 0;
        #endregion variant

        //=====================================
        #region form
        public FrmDieuChuyenKho()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmDieuChuyenKho_Load);
            this.Shown += new EventHandler(FrmDieuChuyenKho_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmDieuChuyenKho_FormClosing);

            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.KeyDown += new KeyEventHandler(FrmDieuChuyenKho_KeyDown);
        }

        void FrmDieuChuyenKho_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else
                if (this.btnSave.Visible && ((e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F6)))
                    Save();
                
        }

        private void FrmDieuChuyenKho_Shown(object sender, EventArgs e)
        {
            this.lookUpMatHang.Focus();
        }

        private void FrmDieuChuyenKho_Load(object sender, EventArgs e)
        {
            Utility.InitControl(this.lookUpMatHang);
            Utility.InitControl(this.lookUpKhoD);
            Utility.InitControl(this.lookUpDonViTinhIDN);
            Utility.InitControl(this.lookUpKhoIDN);

            RefreshData();
        }

        private void FrmDieuChuyenKho_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            FrmDieuChuyenKho.daluu = 0;
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        #endregion grid

        //=====================================
        #region function
        private void RefreshData()
        {
            MatHangBOList MatHangBOList = MatHangBOList.GetMatHangBOList();
            Utility.SetData(this.lookUpMatHang, MatHangBOList, MatHangBOList.Count);

            DanhMucBOList danhMucKhoBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DanhMucKho);
            Utility.SetData(this.lookUpKhoIDN, danhMucKhoBOList, danhMucKhoBOList.Count);
            Utility.SetData(this.lookUpKhoD, danhMucKhoBOList, danhMucKhoBOList.Count);

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.lookUpDonViTinhIDN, donViTinhBOList, donViTinhBOList.Count);

            if (MaVachBO != null)
            {
                txtMaSo.Text = MaVachBO.MaVach.Trim();
                tonKhoBON = TonKhoBO.GetRootTonKhoBO(StaticUtil.GetInt(MaVachBO.TonKhoID.Value, -1));
                lookUpMatHang.EditValue = StaticUtil.GetInt(tonKhoBON.MatHangID.Value, -1);

                lookUpDonViTinhIDN.EditValue = StaticUtil.GetInt(tonKhoBON.DonViTinhID.Value, -1);
                lookUpKhoIDN.EditValue = StaticUtil.GetInt(tonKhoBON.DanhMucKhoID.Value, -1);
                soluongNguon = StaticUtil.GetDouble(MaVachBO.SoLuongTon.Value, 0);
                spinSoLuongN.Value = (decimal)soluongNguon;

                tonKhoBOD = TonKhoBO.GetRootTonKhoBOByWhere("MatHangID = " + StaticUtil.GetInt(tonKhoBON.MatHangID.Value, -1).ToString() +
                    " and DonViTinhID = " + StaticUtil.GetInt(tonKhoBON.DonViTinhID.Value, -1).ToString() +
                    " and DanhMucKhoID = " + StaticUtil.GetInt(lookUpKhoD.EditValue, -1).ToString());

                if (StaticUtil.GetInt(tonKhoBOD.TonKhoID.Value, -1) < 0)
                {
                    tonKhoBOD = TonKhoBO.NewRootTonKhoBO();

                    soluongDich = 0;
                    spinSoLuongD.Value = 0;
                    tonKhoBOD.MatHangID = StaticUtil.GetInt(tonKhoBON.MatHangID.Value, -1);
                    tonKhoBOD.DonViTinhID = StaticUtil.GetInt(tonKhoBON.DonViTinhID.Value, -1);
                    tonKhoBOD.DanhMucKhoID = StaticUtil.GetInt(lookUpKhoD.EditValue, -1);
                    tonKhoBOD.SoLuongTon = 0;

                    MaVachBOD = MaVachBO.NewRootMaVachBO();
                    MaVachBOD.MaVach = txtMaSo.Text.Trim();
                    MaVachBOD.SoLuongTon = 0;
                    soluongDich = 0;
                    spinSoLuongD.Value = 0;
                }
                else
                {
                    MaVachBOD = MaVachBO.GetRootMaVachBOByWhere("TonKhoID =" + tonKhoBOD.TonKhoID.ToString() + " and MaVach = " + txtMaSo.Text.Trim());

                    if (StaticUtil.GetInt(MaVachBOD.MaVachID.Value, -1) < 0)
                    {
                        MaVachBOD = MaVachBO.NewRootMaVachBO();
                        MaVachBOD.MaVach = txtMaSo.Text.Trim();
                        MaVachBOD.SoLuongTon = 0;
                        soluongDich = 0;
                        spinSoLuongD.Value = 0;
                    }
                    else
                    {
                        soluongDich = StaticUtil.GetDouble(MaVachBOD.SoLuongTon.Value, 0);
                        spinSoLuongD.Value = (decimal)soluongDich;
                    }

                }
            }
        }

        private void Save()
        {
            try
            {
                tonKhoBON.SoLuongTon = tonKhoBON.SoLuongTon.Value - soluongChuyen;
                tonKhoBON.Save();
                MaVachBO.SoLuongTon = StaticUtil.GetDouble(spinSoLuongN.Value, soluongNguon);
                MaVachBO.Save();

                tonKhoBOD.SoLuongTon = tonKhoBOD.SoLuongTon.Value + soluongChuyen;
                tonKhoBOD.Save();
                MaVachBOD.SoLuongTon = StaticUtil.GetDouble(spinSoLuongD.Value, soluongDich);
                MaVachBOD.TonKhoID = StaticUtil.GetInt(tonKhoBOD.TonKhoID.Value, -1);
                MaVachBOD.Save();

                ChuyenKhoBO chuyenKhoBO = ChuyenKhoBO.NewRootChuyenKhoBO();
                chuyenKhoBO.MatHangID = StaticUtil.GetInt(tonKhoBON.MatHangID.Value, -1);
                chuyenKhoBO.TonKhoIDN = StaticUtil.GetInt(tonKhoBON.TonKhoID.Value, -1);
                chuyenKhoBO.DonViTinhIDN = StaticUtil.GetInt(tonKhoBON.DonViTinhID.Value, -1);
                chuyenKhoBO.KhoIDN = StaticUtil.GetInt(tonKhoBON.DanhMucKhoID.Value, -1);
                chuyenKhoBO.TonKhoIDD = StaticUtil.GetInt(tonKhoBOD.TonKhoID.Value, -1);
                chuyenKhoBO.DonViTinhIDD = StaticUtil.GetInt(tonKhoBOD.DonViTinhID.Value, -1);
                chuyenKhoBO.MatHangIDD = StaticUtil.GetInt(tonKhoBOD.MatHangID.Value, -1);
                chuyenKhoBO.KhoIDD = StaticUtil.GetInt(tonKhoBOD.DanhMucKhoID.Value, -1);
                chuyenKhoBO.SoLuongChuyen = soluongChuyen;
                chuyenKhoBO.SoLuongQuyDoi = soluongChuyen;
                chuyenKhoBO.DienGiai = "Chuyển - Mặt hàng " + lookUpMatHang.Text.Trim() + " trong " + lookUpKhoIDN.Text.Trim() + " vào " + lookUpKhoD.Text.Trim();
                chuyenKhoBO.Save();

                FrmDieuChuyenKho.daluu = 1;
                this.Close();
            }
            catch
            {

            }
        }
        #endregion function


        private void spinSoLuongChuyen_EditValueChanged(object sender, EventArgs e)
        {
            soluongChuyen = (double)spinSoLuongChuyen.Value;
            double slConLai = soluongNguon - soluongChuyen;
            if (slConLai >= 0)
            {
                spinSoLuongN.Value = (decimal)slConLai;
                spinSoLuongD.Value = (decimal)soluongDich + (decimal)soluongChuyen;
            }
            else
            {
                MessageBox.Show("Số lượng chuyển quá số lượng còn trong Kho Nguồn! ", "Cảnh báo! ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                spinSoLuongChuyen.Value = 0;
                spinSoLuongChuyen.Focus();
            }
        }

        private void lookUpKhoD_EditValueChanged(object sender, EventArgs e)
        {
            RefreshData();
            spinSoLuongChuyen_EditValueChanged(e, e);
        }
    }
}